#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int dx[4] = { 1, -1, 0, 0 };
    int dy[4] = { 0, 0, 1, -1 };
    bool bp[301][301];
    int ret = 0, cur = 0;
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        for (int i = 0; i < grid.size(); i++)
            for (int j = 0; j < grid[i].size(); j++)
                if (grid[i][j] == 1 && bp[i][j] == false)
                {
                    cur = 0;
                    bp[i][j] = true;
                    dfs(grid, i, j);
                    ret = max(ret, cur);
                }
        return ret;
    }
    void dfs(vector<vector<int>>& grid, int x, int y)
    {
        cur++;
        for (int k = 0; k < 4; k++)
        {
            int a = x + dx[k], b = y + dy[k];
            if (a >= 0 && a < grid.size() && b >= 0 && b < grid[a].size() && grid[a][b] == 1 && bp[a][b] == false)
                bp[a][b] = true, dfs(grid, a, b);
        }
    }
};